﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using NewRetail.Application.Components.WeChat.Services;
using NewRetail.Application.Core;
using System.Threading.Tasks;
using System.IO;
using System.Text;
using Microsoft.Extensions.Logging;
using NewRetail.Application.Components.WeChat.Model;
using System;
using NewRetail.Application.Utility;
using System.Text.Json;
using NewRetail.Application.Components.WeChat.Menu.Services;
using System.Collections.Generic;
using SKIT.FlurlHttpClient.Wechat.Api.Models;

namespace NewRetail.Api.Controllers.WeChatControllers
{
    /// <summary>
    /// 用于处理微信菜单的信息
    /// </summary>
    [NewRetailRoute("wechat-menu")]
    public class WeChatMenuController : BaseController
    {
        #region Field
        private readonly IWeChatMenuService _weChatMenuService;
        private readonly ILogger _logger;
        #endregion

        #region Constructor
        public WeChatMenuController(IWeChatMenuService weChatMenuService, ILogger<WeChatMenuController> logger)
        {
            _weChatMenuService = weChatMenuService;
            _logger = logger;
        }
        #endregion

        #region method

        #region 创建微信公众号菜单
        /// <summary>
        /// 创建微信公众号菜单
        /// </summary>
        /// <param name="memus"></param>
        /// <returns></returns>
        [HttpPost("createwechatmenu")]
        public async Task<ResponseEntity<string>> CreateWeChatMenuAsync([FromBody] List<CgibinMenuCreateRequest.Types.Button> memus)
        {
            var retResult = new ResponseEntity<string>();
            try
            {
                var result = await _weChatMenuService.CreateWeChatMenu(memus);
                if (!string.IsNullOrEmpty(result.Item2))
                    retResult.SetError(result.Item2);
                else
                    retResult.SetData(result.Item1);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                retResult.SetError(e.Message);
            }

            return retResult;
        }
        #endregion


        #endregion
    }
}
